package anonymouschat.client.frames;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.SwingConstants;

/**
 * Chat room member Panel shows current members in the chat room.
 * 
 * Right click a name, show chat log with this user.
 * 
 * Left click a name, put the name into chat room's dst text box.
 * 
 * Upon created, it sends a ChatMemberRequest message to server, wait for a
 * ChatMember message.
 * 
 * Refresh by sending and receiving the above messages in some condition.
 * 
 * @author hb
 * 
 */
public class ChatMemberPanel extends JPanel implements ActionListener
{
    /**
     * Default serial
     */
    private static final long serialVersionUID = 1L;

    private ChatFrame chatFrame;

    private List<String> memberList;

    private JPanel sendMsgPanel;

    private JPanel buttonPanel;

    private JPanel mainPanel;

    private JButton[] sendMsgButton;

    private List<JButton> sendMsgButtonList;

    private List<JButton> showHistoryButtonList;

    public ChatMemberPanel( ChatFrame chatFrame, String chatName ) {
        super();
        this.chatFrame = chatFrame;

        this.setLayout( new GridLayout( 0, 1, 0, 0 ) );
        this.add( new JLabel( "Members in '" + chatName + "'" ) );

        mainPanel = new JPanel();
        mainPanel.setLayout( new FlowLayout( 0, 0, 0 ) );
        buttonPanel = new JPanel();
        buttonPanel.setLayout( new GridLayout( 1, 2, 0, 0 ) );

        // sendMsg Panel
        sendMsgPanel = new JPanel();
        sendMsgPanel.setLayout( new GridLayout( 0, 1, 0, 0 ) );
        sendMsgPanel.add( new JSeparator( SwingConstants.HORIZONTAL ) );

        buttonPanel.setPreferredSize( new Dimension( 250, 2 * 50 ) );

        buttonPanel.add( sendMsgPanel );

        mainPanel.add( buttonPanel );
        this.add( mainPanel );

    }

    public void setMemberList( List<String> memberList ) {

        sendMsgPanel.removeAll();
        sendMsgPanel.validate();
        sendMsgButton = new JButton[memberList.size()];
        sendMsgButtonList = new ArrayList<JButton>();
        for( int i = 0; i < memberList.size(); i++ ) {
            chatFrame.showMessage( "User: " + memberList.get( i ) );
            sendMsgButton[i] = new JButton( memberList.get( i ) );
            sendMsgButton[i].addActionListener( this );
            sendMsgButtonList.add( sendMsgButton[i] );
            sendMsgPanel.add( sendMsgButton[i] );
        }
        sendMsgPanel.validate();
    }

    @Override
    public void actionPerformed( ActionEvent e ) {
        for( int i = 0; i < memberList.size(); i++ ) {
            if( e.getSource().equals( sendMsgButtonList.get( i ) ) ) {
                chatFrame.addChatDst( memberList.get( i ) );
                return;
            }
        }
        for( int i = 0; i < memberList.size(); i++ ) {
            if( e.getSource().equals( showHistoryButtonList.get( i ) ) ) {
                new LogFrame( memberList.get( i ) );
                return;
            }
        }
    }
}
